<?php
/**
 * @title 1418. 点菜展示表 - 哈希
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param String[][] $orders
     * @return String[][]
     */
    function displayTable($orders) {
        /**
         * @since 2021-07-06 菜品和餐桌
         */
        $foodArray = $tableArray = [];

        /**
         * @since 2021-07-06 遍历
         */
        foreach ($orders as list($customerName, $tableNumber, $foodItem)){
            $foodArray[$foodItem] = 1;
            if(isset($tableArray[$tableNumber][$foodItem])){
                $tableArray[$tableNumber][$foodItem] += 1;
            } else {
                $tableArray[$tableNumber][$foodItem] = 1;
            }
        }
        // return $tableArray;

        /**
         * @since 2021-07-06 菜品排序
         */
        $foodItemArray = array_keys($foodArray);
        sort($foodItemArray);

        /**
         * @since 2021-07-06 返回数据
         */
        $result = [];
        $result[] = array_merge(["Table"], $foodItemArray);

        /**
         * @since 2021-07-06 餐桌排序
         */
        $tableNumberArray = array_keys($tableArray);
        sort($tableNumberArray);

        /**
         * @since 2021-07-06 每桌点的菜品
         */
        foreach ($tableNumberArray as $tableNumber){
            $tmpArray = [strval($tableNumber)];

            /**
             * @since 2021-07-06 每个菜品数量
             */
            foreach ($foodItemArray as $foodItem){
                if(isset($tableArray[$tableNumber][$foodItem])){
                    $tmpArray[] = strval($tableArray[$tableNumber][$foodItem]);
                } else {
                    $tmpArray[] = "0";
                }
            }
            $result[] = $tmpArray;
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202107/2021.07.06.php
 */
$datas = [
    [[["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->displayTable(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
